[LINQ] Master – Detail Same Record(II)

Posted by JTorrecilla on Geeks with Blogs See other posts from Geeks with Blogs or by JTorrecilla
Published on Thu, 17 Feb 2011 12:04:04 GMT Indexed on 2011/02/17 15:26 UTC
Read the original article Hit count: 193

Filed under:

In my previous post, I introduced my problem, but I didn’t explain the problem with Entity Framework

When you try the solution indicated you will take the following error:

LINQ to Entities don’t recognize the method 'System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])’ of the method, and this method can’t be translated into a stored expression.

The query that produces that error was:

   1: var consulta = (from TCabecera cab in 
   2:                  contexto_local.TCabecera  
   3:    let Detalle = (from TDetalle detalle 
   4:                    in cab.TDetalle 
   5:    select detalle.Nombre)    
   6:    let Nombres = string.Join(",",Detalle )     
   7:    select new   
   8:       {  
   9:        cab.Campo1, 
  10:        cab.Campo2, 
  11:        Nombres
  12:        }).ToList();
  13: grid.DataSource=consulta;

 

Why is this error happening?

This error happens when the query couldn’t be translated into T-SQL.

Solutions?

To quit that error, we need to execute the query on 2 steps:

   1: var consulta = (from TCabecera cab in   
   2:                 contexto_local.TCabecera    
   3:       let Detalle = (from TDetalle detalle   
   4:                      in cab.TDetalle   
   5:                      select detalle.Nombre)      
   6:      select new     
   7:       {    
   8:         cab.Campo1, 
   9:         cab.Campo2,  
  10:         Detalle 
  11:         }).ToList();  
  12: var consulta2 = (from dato in consulta
  13:             let Nombes = string.Join(",",dato.Detalle)
  14:             select new 
  15:             {
  16:             dato.Campo1,
  17:             dato.Campo2,
  18:             Nombres
  19:             };
  20:         grid.DataSource=consulta2.ToList();

Curiously

This problem happens with Entity Framework but, the same problem can’t be reproduced on LINQ – To – SQL, that it works fine in one unique step.

Hope It’s helpful

Best Regards

© Geeks with Blogs or respective owner